package weiyao.xinxidasai.mapper;

import org.apache.ibatis.annotations.*;
import weiyao.xinxidasai.pojo.FilesFolder;

import java.util.List;
import java.util.Map;

@Mapper
public interface FolderMapper {
    @Insert("INSERT INTO files_folder(user_id, folder_path,  create_time) " +
            "VALUES(#{userId}, #{folderPath},  now())")
    @Options(useGeneratedKeys = true, keyProperty = "folderId")
    int insertFolder(FilesFolder folder);


    @Delete("DELETE FROM files_folder WHERE folder_id = #{folderId} AND user_id = #{userId}")
    int deleteFolder(@Param("folderId") Long folderId, @Param("userId") Long userId);

    @Update("UPDATE files_folder SET folder_path = #{newPath} " +
            "WHERE folder_id = #{folderId} AND user_id = #{userId}")
    int updateFolderPath(@Param("folderId") Long folderId,
                         @Param("userId") Long userId,
                         @Param("newPath") String newPath);

    @Select("SELECT * FROM files_folder WHERE folder_id = #{folderId}")
    FilesFolder selectFolderById(Long folderId);

    @Select("SELECT * FROM files_folder WHERE user_id = #{userId}")
    List<FilesFolder> selectFoldersByUserId(Long userId);
@Select("SELECT * FROM files_folder WHERE folder_id = #{folderId}")
    FilesFolder selectById(Long folderId);
    @Select("SELECT * FROM files_folder WHERE user_id = #{userId}")
    List<FilesFolder> findFolderByUserId(Long userId);

    @Select("SELECT * FROM files_folder WHERE user_id = #{userId} AND folder_path=#{folderPath}")
    FilesFolder selectByPathanduser(@Param("userId") Long userId,@Param("folderPath") String folderPath);

    @Select("SELECT * FROM files_folder WHERE user_id = #{userId}")
    List<FilesFolder> findByUserId(@Param("userId") Long userId);


    @Select("SELECT folder_path FROM files_folder WHERE folder_id = #{folderId}")
    String findPathById(@Param("folderId") Long folderId);

    @Select("SELECT folder_id, folder_path FROM files_folder " +
            "WHERE folder_id IN (#{folderIds})")
    Map<Long, String> getFolderPathsForFiles(
            @Param("folderIds") List<Long> folderIds);

    @Select("SELECT * FROM files_folder " +
            "WHERE folder_path LIKE CONCAT(#{basePath}, '/%') " +
            "AND folder_path NOT LIKE CONCAT(#{basePath}, '/%/%') " +
            "AND user_id = #{userId}")
    List<FilesFolder> selectSubFoldersByPath(
            @Param("basePath") String basePath,
            @Param("userId") Long userId);


    @Select("SELECT * FROM files_folder " +
            "WHERE user_id = #{userId} AND folder_path LIKE CONCAT('%', #{keyword}, '%')")
    List<FilesFolder> findFoldersByKeyword(
            @Param("userId") Long userId,
            @Param("keyword") String keyword);



}
